/*
	vehModles contains the models for the service
*/
package vehModels

import (
	// "fmt"
	"reflect"
	"labix.org/v2/mgo/bson"
)

//** TYPES

type (

	VehicleDetail struct {
		ID		   bson.ObjectId `bson:"_id,omitempty"`
		VehicleId  string        `bson:"VehicleId" form:"VehicleId" detail:"车牌"`
		Date	   string        `bson:"Date" form:"Date" detail:"日期"`
		Type	   string        `bson:"Type" form:"Type" detail:"车型"`
		Brand	   string        `bson:"Brand" form:"Brand" detail:"型号"`
		Color	   string        `bson:"Color" form:"Color" detail:"颜色"`
	}

	
	// vehicle info contains information for an individual vehicle
	VehicleInfo struct {
		ID		   bson.ObjectId `bson:"_id,omitempty" form:"ID"`
		VehicleId  string        `bson:"VehicleId" form:"VehicleId" detail:"车牌"`
		Date	   string        `bson:"Date" form:"Date" detail:"日期"`
		Type	   string        `bson:"Type" form:"Type" detail:"车型"`
		Brand	   string        `bson:"Brand" form:"Brand" detail:"型号"`
		Color	   string        `bson:"Color" form:"Color" detail:"颜色"`
//		VehicleDetail			// 车辆的详细信息
		// 维护信息
		// 投保信息
		// 用油信息
	}

	VehicleInfoForm struct {
		ID string `form:"ID" valid:"Required; MinSize(4)" error:"invalid_id"`
		Vehid string `form:"VehicleId" valid:"MinSize(4)" error:"invalid_vehicle_id"`
		Date string `form:"Date" valid:"MinSize(4)" error:"invalid_date"`
		Type string `form:"Type" valid:"MinSize(2)" error:"invalid_type"`
		Brand string `form:"Brand" valid:"MinSize(2)" error:"invalid_brand"`
		Color string `form:"Color" valid:"MinSize(1)" error:"invalid_color"`
	}

	VehicleInfoArray struct {
		Title   string			// 显示用名字
		Name    string			// 成员在内部用的名字
		Value   string			// 值
	}
)

func (vehicleInfo *VehicleInfo) ToMapString() (result map[string]interface{}) {
	result = map[string]interface{}{}

	vt := reflect.TypeOf(*vehicleInfo)
	vv := reflect.ValueOf(*vehicleInfo)
    for i := 0; i < vt.NumField(); i++ {
        f := vt.Field(i)
		chKey := f.Tag.Get("detail")
		if chKey == "" {
			continue
		}
		result[chKey] = vv.FieldByName(f.Name).String()
	}
	return result
}

func (info *VehicleInfo) ToFormStruct() (form VehicleInfoForm) {
	form = VehicleInfoForm{}

	form.ID = info.ID.Hex()
	form.Vehid = info.VehicleId
	form.Date  = info.Date
	form.Type  = info.Type
	form.Brand = info.Brand
	form.Color = info.Color
	return form
}

func (vehicleInfo *VehicleInfo) ToInfoArray() (result []VehicleInfoArray) {
	vt := reflect.TypeOf(*vehicleInfo)
	vv := reflect.ValueOf(*vehicleInfo)
	
	result = make([]VehicleInfoArray, vt.NumField())
	
    for i := 0; i < vt.NumField(); i++ {
        f := vt.Field(i)
		chKey := f.Tag.Get("detail")
		result[i].Title = chKey
		result[i].Name  = f.Name
		if f.Name == "ID" {
			objid, ok := vv.FieldByName(f.Name).Interface().(bson.ObjectId)
			if ok {
				result[i].Value = objid.Hex()
			}
		} else {
			result[i].Value = vv.FieldByName(f.Name).String()
		}
	}
	
	return result
}

func (infoform *VehicleInfoForm) ToInfoStruct() (result VehicleInfo) {
	result = VehicleInfo{}

	if bson.IsObjectIdHex(infoform.ID) {
		result.ID = bson.ObjectIdHex(infoform.ID)
	}
	result.VehicleId = infoform.Vehid
	result.Date  = infoform.Date
	result.Type  = infoform.Type
	result.Brand = infoform.Brand
	result.Color = infoform.Color

	return result
}



// func (vehCondition *VehCondition) DisplayWindSpeed() string {
// 	return fmt.Sprintf("%.2f", vehCondition.WindSpeed)
// }

// func (vehCondition *VehCondition) DisplayWindGust() string {
// 	return fmt.Sprintf("%.2f", vehCondition.WindGust)
// }




